if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");

var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

 

function task() {
  let _newJoin= threads.start(function () {
      while(true){
        var tv_join = id("tv_immediately_join").findOne(1000);
        if(tv_join){
          tv_join.click();
          var copy = text("复制口令").findOne(5000);
          if(copy){
            copy.click();
            _newJoin.interrupt();
            break ;

          }
        }
    }     
  });

  //签到
  var 签到 = text("立即签到").findOne(10000);
  while(true){
    log("签到:" + 签到)
    if(签到){
      签到.click();
      log("签到完成.")
      var close = id("com.androidapp.mibo:id/iv_sign_dialog_close").findOne(2000);
      if(close){
        close.click();
        utils.updateAppProgress(40);
        break;
      }
    }else{
      toastLog('未找到签到');
      return ;
    }
    var qd = id("com.androidapp.mibo:id/iv_gifts").findOne(2000);
    if(qd){
      qd.click()
      sleep(2000)
      var yqd = text("已签到").findOne(1000);
      if(yqd){
        log("已签到.")
        var close = id("com.androidapp.mibo:id/iv_close").findOne(2000);
        if(close){
          utils.updateAppProgress(40);
          close.click();
          break ;
        }
         close = id("com.androidapp.mibo:id/iv_sign_dialog_close").findOne(2000);
        if(close){
          utils.updateAppProgress(40);  
          close.click();
          break ;
        }
        
      }
    }
    sleep(1000)
    签到 = text("立即签到").findOne(1000);
  }
  _newJoin.interrupt();
  utils.updateAppProgress(60);
  var 推荐 = text("推荐").findOne(5000)
  log("点击推荐")
  if (推荐) {
    sleep(20000)
    var index = 1
    var count = 50
    while (index <= count) {
      var tv_task_tip_play = text("查看商业视频").findOne(1000);
      if(tv_task_tip_play){
        for(var i = 0;i<2;i++){
          if(!tv_task_tip_play){
            toastLog("查看商业视频任务完成.")
            break ;
          }
          var finalActivity = currentActivity()
          tv_task_tip_play.click();
          sleep(5000)
          utils.waitAdvert(finalActivity);
          sleep(2000)
          tv_task_tip_play = text("查看商业视频").findOne(1000);
          
        }
        utils.updateAppProgress(80);
      }

      var check_obj = id("com.androidapp.mibo:id/flt_countdown").findOne(1000) ||
        id("com.androidapp.mibo:id/cp_countdown2").findOne(1000)
      if (!check_obj) {
        toastLog("观看视频任务已经完成")
        sleep(time)

        break;
      }
      var random_time = random(10000, 15000)
      toastLog("观看第" + index + "/" + count + "个视频中...")
      sleep(time)
      toastLog("预计时长" + (random_time / 1000) + "秒~")
      sleep(random_time)      
      utils.swip_up();
      index++
      if(index*5 < 100){
        utils.updateAppProgress(index*5);
      }      
    }
    //任务完成弹框关闭
    var close_btn = id("iv_taskprogress_close").findOne(2000);
    if(close_btn) close_btn.click()
  }else{
    toastLog('未找到推荐');
    rethrn ;
  }
}

function main() {
  let open_app = launchAppEnhance("天音短视频");
  if (open_app) {
    task()
  }
  else {
    toastLog("天音短视频打开失败，请确认是否安装并授权打开!");
  }
}


try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
  
}

storage.put("script_running_status", "end");
